

import 'package:flutter/material.dart';
import 'package:huajianshe_app/base/BaseState.dart';
import 'package:huajianshe_app/base/Refresher.dart';
import 'package:huajianshe_app/common/C.dart';
import 'package:huajianshe_app/common/TS.dart';
import 'package:huajianshe_app/common/load_image.dart';
import 'package:huajianshe_app/common/space.dart';
import 'package:huajianshe_app/network/Api.dart';
import 'package:huajianshe_app/pages/circle/model/CircleModel.dart';
import 'package:huajianshe_app/pages/main/model/MomentModel.dart';
import 'package:huajianshe_app/utils/dialog_util.dart';
import 'package:huajianshe_app/widget/RoundedCornerWidget.dart';
import 'package:huajianshe_app/widget/SmartButton.dart';
import 'MomentDetailPage.dart';
import 'MomentItem.dart';

//首页推荐/好友列表
class MomentListPage extends StatefulWidget {

  final int momentType;
  final int? businessType; //动态类型 1.动态 2.商机 （默认全部）
  final int? circleId;
  final ValueNotifier<int>? mainIndexNotifier;
  final bool initNotShowProgress;

  MomentListPage({Key? key,
    this.momentType = MomentType.MOMENT_TYPE_RECOMMENDATION,
    this.businessType,
    this.circleId,
    this.mainIndexNotifier,
    this.initNotShowProgress = false
  }) : super(key: key);

  @override
  State<StatefulWidget> createState() {
    return _MomentListPageState();
  }
}

class _MomentListPageState extends BaseState<MomentListPage> {

  int page = 1;

  @override
  void initState() {
    super.initState();

    refresher = Refresher<MomentListData>(this);
    // List<String> list = ['1', '2', '2', '2', '2', '2', '2', '2'];
    // refresher?.data = list;
    load(widget.initNotShowProgress);

  }
  @override
  Widget build(BuildContext context) {
    return ColoredBox(color: Colors.transparent, child: Padding(padding: EdgeInsets.symmetric(horizontal: 15), child:
      refresher?.build(buildChild())
    ));
  }

  Widget? buildChild() {
    Widget? child = buildEmptyPage();
    if (child != null) {
      return child;
    }
    return
      ListView.separated(
          itemBuilder: (_, index) {
            CircleInfo? circleInfo = getCircle(refresher?.data?[index]);
            return MomentItem(refresher?.data?[index], circleInfo, onComment: onComment);
          },
          separatorBuilder: (_, index) => H(10),
          itemCount: (refresher?.data?.length ?? 0)
      );
  }

  void onComment(MomentListData? data) {
    DialogUtil.showBottomDialogV2(context, MomentDetailPage(data, getCircle(data)), height: 0.8,
        radius: 13, cornerPosition: 3, margin: EdgeInsets.symmetric(horizontal: 5));
  }

  CircleInfo? getCircle(MomentListData? data) {//如果是推荐列表，需要取出圈子信息传到个人主页
    CircleInfo? circleInfo;
    if (widget.momentType == MomentType.MOMENT_TYPE_RECOMMENDATION) {
      if ((data?.circles?.length ?? 0) > 0) {
        circleInfo = data?.circles?[0];
      }
    }
    return circleInfo;
  }
  
  void load(bool doNotShowProgressBar) {
    int? type = widget.businessType;
    if (widget.momentType == MomentType.MOMENT_TYPE_RECOMMENDATION ) {//推荐只有动态，好友里还有商机
      type = MomentBusinessType.MOMENT;
    }
    var data = {
      'page': page,
      'list_rows': 10,
      'get_type':  widget.momentType,
      'type': type,
      'circle_id': widget.circleId,
    };
    sendReq(Api.momentList(), MomentListResp(), data: data, doNotShowProgressBar: doNotShowProgressBar);
  }

  @override
  void onRefresh() {
    refresher?.mode = Mode.refresh;
    page = 1;
    load(true);
  }

  @override
  void onLoadMore() {
    print('onLoadMore');
    refresher?.mode = Mode.loadMore;
    load(true);
  }

  @override
  void onReqSuccess<T>(T t, int code, String msg, String path) {
    super.onReqSuccess(t, code, msg, path);
    if (t is MomentListResp) {
      List<MomentListData>? data = t.data?.data;
      refresher?.load(data);
      setState(() { });
      page++;
    }
  }

  @override
  Widget? buildEmptyView() {
    if (widget.momentType == MomentType.MOMENT_TYPE_FRIEND) {
      return Center(child: RoundedCornerWidget(
        child: Column(mainAxisSize: MainAxisSize.min, children: [
          H(22),
          LoadImage('no_circles_img'),
          H(12),
          Text('没有任何动态哦~', style: TS(16, C.gray333),),
          H(5),
          Text('加入圈子，获取更多动态吧', style: TS(14, C.gray666)),
          H(25),
          SmartButton(width: 265,
            height: 40,
            text: '查看热门圈子',
            fontSize: 15,
            textColor: Colors.white,
            radius: 20,
            onPressed: () { widget.mainIndexNotifier?.value = 2; },
          ),
          H(22)
        ]),
        borderWidth: 0,
        radius: 13,
        width: 315,
        height: 250,
        gradient: LinearGradient(colors: [Color(0xFFFFEBED), Color(0xFFFFFFFF)],
            begin: Alignment.topCenter,
            end: Alignment.bottomCenter),));
    } else {
      return super.buildEmptyView();
    }
  }
}